package com.fick.struts.action;
/**
 * 角色管理
 * add by 王浩强
 */
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.fick.hibernate.dao.RoleDAO;
import com.fick.hibernate.model.Role;



public class RoleAction extends DispatchAction {

	private static final Log log = LogFactory.getLog(RoleAction.class);
	
	private int id=0;
	private String roleName="";
	private int roleOrder=0;
	private RoleDAO dao=new RoleDAO();
	
	private void initPara(HttpServletRequest request){
		if (request.getParameter("id")!=null && request.getParameter("id").trim().length()>0) {
			try {
				id=Integer.parseInt(request.getParameter("id"));
			} catch (Exception e) {
				id=0;
			}
		}
		if (request.getParameter("roleName")!=null && request.getParameter("roleName").trim().length()>0) {
			roleName=request.getParameter("roleName");
		}
		if (request.getParameter("roleOrder")!=null ) {
			try {
				roleOrder=Integer.parseInt(request.getParameter("roleOrder"));
			} catch (Exception e) {
				roleOrder=0;
			}
		}
	}
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		initPara(request);
		Role role=new Role();
		role.setRoleName(roleName);
		role.setRoleOrder(roleOrder);
		dao.save(role);
		return mapping.findForward("role");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		initPara(request);
		Role role=dao.findById(id);
		if (role!=null) {
			role.setRoleName(roleName);
			role.setRoleOrder(roleOrder);
			dao.update(role);
		}
		return mapping.findForward("role");
	}
	public ActionForward del(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		initPara(request);
		Role role=dao.findById(id);
		if (role!=null) {
			dao.delete(role);
		}
		return mapping.findForward("role");
	}
}
